<?php
Loader::loadFile('config.php', "modules/InnoForum");

Loader::loadClass('DateUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('ObjectUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('PNObjectEx', "modules/InnoForum/pnincludes");
Loader::loadClass('PNObjectExArray', "modules/InnoForum/pnincludes");
Loader::loadClass('DBUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('SecurityUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('InnoUtil', "modules/InnoForum/pnincludes");

/**
 * initialise block
 * 
 * @author       The PostNuke Development Team
 */
function InnoForum_SameTopicsblock_init()
{

}

/**
 * get information on block
 * 
 * @author       The PostNuke Development Team
 * @return       array       The block information
 */
function InnoForum_SameTopicsblock_info()
{
    return array('text_type'      => 'Same topics ',
                 'module'         => 'InnoForum',
                 'text_type_long' => 'Show same topic',
                 'allow_multiple' => true,
                 'form_content'   => false,
                 'form_refresh'   => false,
                 'show_preview'   => true);
}

/**
 * display block
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the rendered bock
 */
function InnoForum_SameTopicsblock_display($blockinfo)
{
    $modname  = 'InnoForum';
    $class    = 'SameTopics';

    $id    = FormUtil::getPassedValue ('id', NULL , 'GET');

    $vars = pnBlockVarsFromContent($blockinfo['content']);
    $samesize = isset($vars['pagesize'])?$vars['pagesize']:20;

    $render = pnRender::getInstance($modname);
    
    //var_dump($id); exit;
    $objectArray = array();
    if($id){
      //////////////////////////////////////
      //Query this topics information
      //////////////////////////////////////
      $pntables = pnDBGetTables();
      $table  = $pntables['innoforum_topics'];
      $column = $pntables['innoforum_topics_column'];


      $sqlThisTopic = "SELECT
                          $table.$column[category_id],  
                          $table.$column[subcategory_id]
                        FROM
                          $table 
                        WHERE
                          $table.$column[id] = ".$id;

      $columnThisTopic = array("category_id", "subcategory_id");
      $resultThisTopic = DBUtil::executeSQL($sqlThisTopic);
      $topicArray = DBUtil::marshallObjects ($resultThisTopic, $columnThisTopic);
      $category_id = $topicArray['0']['category_id'];
      $subcategory_id = $topicArray['0']['subcategory_id'];

      //////////////////////////////////////
      //Query Same Topics Of User
      /////////////////////////////////////
      $userLimit = ($samesize-2);
      $sqlSameTopics = "SELECT
                          $table.$column[id],  
                          $table.$column[title],  
                          $table.$column[price],  
                          $table.$column[price_detail],  
                          $table.$column[cr_uid],  
                          $table.$column[firstimage],  
                          $table.$column[firsthumbimage]  
                        FROM
                          $table 
                        WHERE
                          $table.$column[category_id] = ".$category_id."
                        AND 
                          $table.$column[subcategory_id] = ".$subcategory_id."
                        AND 
                          $table.$column[type_id] != 3 
                        AND 
                          $table.$column[cr_uid] != 2 
                        ORDER BY 
                          RAND() 
                        LIMIT ".$userLimit
                      ;

      $columnSameTopics = array("id", "title", "price", "price_detail", "cr_uid", "firstimage", "firsthumbimage");
      $resultSameTopics = DBUtil::executeSQL($sqlSameTopics);
      $sameTopicsArray = DBUtil::marshallObjects ($resultSameTopics, $columnSameTopics);


      //////////////////////////////////////
      //Query Same Topics Of Admin
      /////////////////////////////////////
      $sqlSameTopicsAdmin = "SELECT
                              $table.$column[id],  
                              $table.$column[title],  
                              $table.$column[price],  
                              $table.$column[price_detail],  
                              $table.$column[cr_uid],  
                              $table.$column[firstimage],  
                              $table.$column[firsthumbimage]  
                            FROM
                              $table 
                            WHERE
                              $table.$column[category_id] = ".$category_id."
                            AND 
                              $table.$column[subcategory_id] = ".$subcategory_id."
                            AND 
                              $table.$column[type_id] != 3 
                            AND 
                              $table.$column[cr_uid] = 2 
                            ORDER BY 
                              RAND() 
                            LIMIT 2 "
                          ;

      $columnSameTopicsAdmin = array("id", "title", "price", "price_detail", "cr_uid", "firstimage", "firsthumbimage");
      $resultSameTopicsAdmin = DBUtil::executeSQL($sqlSameTopicsAdmin);
      $sameTopicsAdminArray = DBUtil::marshallObjects ($resultSameTopicsAdmin, $columnSameTopicsAdmin);

      $tempObjectArray = $sameTopicsAdminArray + $sameTopicsArray;
    }

    /////////////////////////////
    //Shuffle array for display
    ////////////////////////////
    $keys = array_keys($tempObjectArray);
    shuffle($keys);
    foreach($keys as $key) {
        $objectArray[] = $tempObjectArray[$key];
    }

    //print_r($objectArray); exit;
    $render->assign('objectArray', $objectArray);

    $blockinfo['content'] = $render->fetch('block_sametopics.htm');
    return themesideblock($blockinfo);
}


/**
 * modify block settings
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the bock form
 */
function InnoForum_SameTopicsblock_modify($blockinfo)
{
  $vars = pnBlockVarsFromContent($blockinfo['content']);
  //load render
  $render = pnRender::getInstance('InnoForum');
  $render->assign('form', $vars);
  
	return $render->fetch('block_sametopics_modify.htm');
}


/**
 * update block settings
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       $blockinfo  the modified blockinfo structure
 */
function InnoForum_SameTopicsblock_update($blockinfo)
{
    
    // Get current content
    $vars = pnBlockVarsFromContent($blockinfo['content']);
    
    // alter the corresponding variable
    $form = FormUtil::getPassedValue ('form', false );
    $vars['pagesize'] = !empty($form['pagesize']) ? $form['pagesize'] : 10;

    // write back the new contents
    $blockinfo['content'] = pnBlockVarsToContent($vars);

    // clear the block cache
    $pnRender = pnRender::getInstance('InnoForum');
    $pnRender->clear_cache('block_sametopics.htm');
    
    return $blockinfo;
}
